package com.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Revenue {
	public static Gson g = new Gson();
	public static void getRevenue(){
		File it_spend = new File("C:/chen/GCG/it_spend.txt");
		File revenue = new File("C:/chen/GCG/revenue.txt");
		File f1 = new File("C:/crystalData/GCG");
		File[] comps = f1.listFiles();
		try {
			BufferedWriter bw1 = new BufferedWriter(new FileWriter(it_spend,true));
			BufferedWriter bw2 = new BufferedWriter(new FileWriter(revenue,true));
			bw1.append("name\tIT_Spend\n");
			bw2.append("name\tyear\tSTG\tSWG\tGTS\tGBS\tIGF\tIBMtotal\tGTS ITS\tGTS MTS\tGBS CSI\ttotalSVCS\n");
			//totalSTG	totalSWG	totalGTS	totalGBS	totalIGF	IBMtotal
			//gtsITS	gtsMA	gbsCNSI	totalSVCS
			int i=0;
			for (File comp : comps) {
				if(comp.isDirectory()){
					File[] meths = comp.listFiles();
					if(meths.length>1){
						File gart = new File(comp.getAbsolutePath()+"/gartner.txt");
						File ship = new File(comp.getAbsolutePath()+"/IBMRelationships.txt");
						String spend = getGartner(gart);
						if(!spend.equals("")){
							bw1.append(comp.getName()+"\t"+spend);
							bw1.append("\n");
						}
						List<Map<String,Double>> years = getShip(ship);
						if(years!=null){
							Map<String,Map<String,Double>> yearMap = toYears(years);
							for(int j=2009;j<2017;j++){
								if(yearMap.get(j+"")!=null){
									Map<String,Double> temp = yearMap.get(j+"");
									bw2.append(comp.getName()+"\t"+j+"\t");
									bw2.append(temp.get("totalSTG")==null?"0\t":(temp.get("totalSTG")+"\t"));
									bw2.append(temp.get("totalSWG")==null?"0\t":(temp.get("totalSWG")+"\t"));
									bw2.append(temp.get("totalGTS")==null?"0\t":(temp.get("totalGTS")+"\t"));
									bw2.append(temp.get("totalGBS")==null?"0\t":(temp.get("totalGBS")+"\t"));
									bw2.append(temp.get("totalIGF")==null?"0\t":(temp.get("totalIGF")+"\t"));
									bw2.append(temp.get("IBMtotal")==null?"0\t":(temp.get("IBMtotal")+"\t"));
									bw2.append(temp.get("gtsITS")==null?"0\t":(temp.get("gtsITS")+"\t"));
									bw2.append(temp.get("gtsMA")==null?"0\t":(temp.get("gtsMA")+"\t"));
									bw2.append(temp.get("gbsCNSI")==null?"0\t":(temp.get("gbsCNSI")+"\t"));
									bw2.append(temp.get("totalSVCS")==null?"0\t":(temp.get("totalSVCS")+"\t"));
									bw2.append("\n");
								}
							}
						}
						
					}
				}
			}
			bw1.close();
			bw2.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static Map<String,Map<String,Double>> toYears(List<Map<String,Double>> years){
		Map<String,Map<String,Double>> map = new HashMap<String,Map<String,Double>>();
		for(int i=0;i<years.size();i++){
			map.put(Math.round(years.get(i).get("year"))+"", years.get(i));
		}
		return map;
	}
	public static String getGartner(File f){
		String result = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			String str = br.readLine();
			br.close();
			if(str.equals("") || str.equals("{}")){
				return "";
			}
			Map<String,Object> map = g.fromJson(str, new TypeToken<Map<String,Object>>(){}.getType());
			if(map.get("gartnerEstimatesProviders")==null){
				return "";
			}
			List<Map<String,Double>> list = (List<Map<String, Double>>) map.get("gartnerEstimatesProviders");
			if(list==null || list.size()==0){
				return "";
			}
//			System.out.println(f.getAbsolutePath());
			result = list.get(0).get("ItSpentThUSD").toString();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	public static List<Map<String,Double>> getShip(File f){
		List<Map<String,Double>> list = new ArrayList<Map<String,Double>>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(f));
			String str = br.readLine();
			br.close();
			if(str.equals("") || str.equals("{}")){
				return null;
			}
			Map<String,Object> map = g.fromJson(str, new TypeToken<Map<String,Object>>(){}.getType());
			if(map.get("IBMDetails")==null || map.get("IBMDetails").equals("{}")){
				return null;
			}
			List<Map<String,Object>> ibmDetail = (List<Map<String, Object>>) map.get("IBMDetails");
			if(ibmDetail==null || ibmDetail.size()==0){
				return null;
			}
			List<Map<String,Double>> years = (List<Map<String, Double>>) ibmDetail.get(0).get("years");
			if(years==null || years.size()==0){
				return null;
			}
			list = years;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public static void main(String[] args) {
//		File f = new File("C:/crystalData/ANZ/PENINSULA HEALTH/*.id");
//		System.out.println(f);
		getRevenue();
	}
}
